package edu.dali.enterprise_test.service.auth.impl;

import edu.dali.enterprise_test.dao.mapper.auth.AuthenticationMapper;
import edu.dali.enterprise_test.dao.mapper.auth.PermissionMapper;
import edu.dali.enterprise_test.model.Pager;
import edu.dali.enterprise_test.model.vo.Dto;
import edu.dali.enterprise_test.model.vo.SysPermission;
import edu.dali.enterprise_test.service.auth.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private AuthenticationMapper authenticationMapper;

    @Override
    public Pager<SysPermission> getPermissions(Dto dto) {
        List<SysPermission> perms =  permissionMapper.getPerms(dto);
        int count = permissionMapper.getPermsCount(dto);
        Pager<SysPermission> p = new Pager<>();
        p.setCode(0);
        p.setMsg("");
        p.setData(perms);
        p.setCount(count);
        return p;
    }

    @Override
    public void insertPermission(SysPermission permission) {
        authenticationMapper.insertPermission(permission);
    }

    @Override
    public void updatePermission(SysPermission permission) {
        authenticationMapper.updatePermission(permission);
    }

    @Override
    public void deletePermission(Long id) {
        authenticationMapper.deletePermission(id);
        Dto dto = new Dto();
        dto.put("permission_id" , id);
        authenticationMapper.deleteRolePermission(dto);
    }

    @Override
    public SysPermission selectPerm(Dto dto) {
        return permissionMapper.selectPerm(dto);
    }


}
